package cn.lizhangqu.kb.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;

import java.io.InputStream;
import java.util.List;

import cn.lizhangqu.kb.R;
import cn.lizhangqu.kb.adapter.LgrgListAdapter;
import cn.lizhangqu.kb.handler.LgrgListJsonResponseHandler;
import cn.lizhangqu.kb.model.LgrgNode;
import cn.lizhangqu.kb.util.Toasts;

/**
 * Created by 柯尊诚 on 2015/11/10.
 * kzc
 */
public class LgrgListActivity extends FragmentActivity {

    private GridView lgrgListGv;
    private List<LgrgNode> lgrgNodes;

    LgrgListJsonResponseHandler handler = new LgrgListJsonResponseHandler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lgrg_list);

        init();
        initEvent();
    }

    private void initEvent() {
        lgrgListGv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toasts.makeText(lgrgNodes.get(position).getTitle());
                Intent intent = new Intent(LgrgListActivity.this, Lgrg2Activity.class);
                intent.putExtra("type", lgrgNodes.get(position).getId());
                startActivity(intent);
            }
        });
    }

    private void init() {
        lgrgListGv = (GridView) findViewById(R.id.lgrg_list_gridView);

        try {
            InputStream in = getAssets().open("lgrg_list.json");

            byte[] data = new byte[in.available()];

            in.read(data);

            Log.d("imeth", new String(data));

            handler.parse(new String(data));

        } catch (Exception e) {
            e.printStackTrace();
        }
        lgrgNodes = handler.getParsedItems();
        LgrgListAdapter adapter = new LgrgListAdapter(getApplicationContext(), R.layout.item_linknode_layout);

        adapter.addItem(lgrgNodes);

        lgrgListGv.setAdapter(adapter);


    }
}
